package com.yxhl.zoume.core.func.pay.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.yxhl.protobuf.BizOrder;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.func.pay.fragment.BusPaymentFragment;
import com.yxhl.zoume.core.main.info.MainEntrance;
import com.yxhl.zoume.core.main.model.MainEntranceInfo;
import com.yxhl.zoume.data.http.model.busticket.ZMBusTicketOrder;
import com.yxhl.zoume.di.component.base.HasComponent;
import com.yxhl.zoume.di.component.pay.DaggerOrderPayComponent;
import com.yxhl.zoume.di.component.pay.OrderPayComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.di.module.pay.OrderPayModule;

/**
 * 订单页面
 * Created by vhawk on 16/3/31.
 */
public class BusPaymentActivity extends BaseActivityWithToolBar implements HasComponent<OrderPayComponent> {
    private static final String INTENT_EXTRA_PAYMENT_ZOUME_BUS_ORDER = "intent_extra_payment_zoume_bus_order";
    private static final String INTENT_EXTRA_PAYMENT_BUS_TICKET_ORDER = "intent_extra_payment_bus_ticket_order";

    OrderPayComponent payComponent;

    /**
     * 适用于定制巴士、固定时刻类巴士
     *
     * @param context
     * @param order
     * @return
     */
    public static Intent getCallingIntent(Context context, BizOrder order) {
        Intent intent = new Intent(context, BusPaymentActivity.class);
        intent.putExtra(INTENT_EXTRA_PAYMENT_ZOUME_BUS_ORDER, order);
        return intent;
    }

    /**
     * 适用于巴士车票
     *
     * @param context
     * @param order
     * @return
     */
    public static Intent getCallingIntent(Context context, ZMBusTicketOrder order) {
        Intent intent = new Intent(context, BusPaymentActivity.class);
        intent.putExtra(INTENT_EXTRA_PAYMENT_BUS_TICKET_ORDER, order);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 定制巴士、固定时刻类巴士
        BizOrder order = (BizOrder) getIntent().getSerializableExtra(INTENT_EXTRA_PAYMENT_ZOUME_BUS_ORDER);
        // 巴士车票
        ZMBusTicketOrder ticketOrder = (ZMBusTicketOrder) getIntent().getSerializableExtra(INTENT_EXTRA_PAYMENT_BUS_TICKET_ORDER);

        if (null != order) {
            addFragment(R.id.fragment_pay_home, BusPaymentFragment.newInstance(order));
        } else if (null != ticketOrder) {
            addFragment(R.id.fragment_pay_home, BusPaymentFragment.newInstance(ticketOrder));
        }
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        MaterialDialog.Builder builder = new MaterialDialog.Builder(this)
                .title(R.string.dialog_title_hint)
                .content(R.string.hint_cancel_payment)
                .positiveText(R.string.dialog_confirm)
                .negativeText(R.string.dialog_cancel);

        final MaterialDialog aDialog = builder.build();
        aDialog.show();

        builder.onPositive(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                BusPaymentActivity.super.onBackPressed();

                aDialog.dismiss();
                getAppComponent().navigator().navigateToMain(BusPaymentActivity.this, new MainEntranceInfo(MainEntrance.PAYMENT));
            }
        }).onNegative(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                aDialog.dismiss();
            }
        });
    }

    @Override
    protected void onHeaderRightClick(View view) {
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_pay;
    }

    @Override
    protected void initializeInjector() {
        payComponent = DaggerOrderPayComponent.builder()
                .appComponent(getAppComponent())
                .activityModule(new ActivityModule(this))
                .orderPayModule(new OrderPayModule())
                .build();
    }

    @Override
    public OrderPayComponent getComponent() {
        return payComponent;
    }
}
